package com.form.question;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.entity.TestQuestion;
import com.exception.CustomException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
 * Version: V1.0    <br/>
 * Datetime:   2022/5/15 17:17   <br/>
 * Description: 添加试题的请求
 *
 * @author: chen
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class AddQuestionForm {

    /**
     * 题目
     */
    private String name;

    /**
     * 题目类型：1-单选题;2-多选题;3-判断题;4-填空题
     */
    private Integer type;

    /**
     * 分值
     */
    private Integer score;

    /**
     * 答案
     */
    private String answer;

    /**
     * 正确答案
     */
    private String correctAnswer;

    /**
     * 分析
     */
    private String analysis;

    public TestQuestion generate() {
        if (StrUtil.isEmpty(this.name)) {
            throw new CustomException("题目不能为空");
        }
        if (this.type == null || this.type < 1 || this.type > 4) {
            throw new CustomException("题目类型不正确");
        }
        if (this.score == null || this.score < 1) {
            throw new CustomException("分值不正确");
        }
        if ((this.type == 1 || this.type == 2) && StrUtil.isEmpty(this.answer)) {
            throw new CustomException("答案不能为空");
        }
        if (StrUtil.isEmpty(this.correctAnswer)) {
            throw new CustomException("正确答案不能为空");
        }
        TestQuestion testQuestion = new TestQuestion();
        BeanUtil.copyProperties(this, testQuestion, CopyOptions.create().setIgnoreNullValue(true));
        return testQuestion;
    }
}